package cn.source.web.controller.school;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.school.domain.School;
import cn.source.school.service.ISchoolService;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.common.core.page.TableDataInfo;

/**
 * 学校列表Controller
 *
 * @author xuhao
 * @date 2023-12-18
 */
@RestController
@RequestMapping("/school/school")
public class SchoolController extends BaseController
{
    @Autowired
    private ISchoolService schoolService;

    /**
     * 查询学校列表列表
     */


    @PreAuthorize("@ss.hasPermi('school:school:list')")
    @GetMapping("/list")
    public TableDataInfo list(School school)
    {
        startPage();
        List<School> list = schoolService.selectSchoolList(school);
        return getDataTable(list);
    }

    /**
     * 导出学校列表列表
     */
    @PreAuthorize("@ss.hasPermi('school:school:export')")
    @Log(title = "学校列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, School school)
    {
        List<School> list = schoolService.selectSchoolList(school);
        ExcelUtil<School> util = new ExcelUtil<School>(School.class);
        util.exportExcel(response, list, "学校列表数据");
    }

    /**
     * 获取学校列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('school:school:query')")
    @GetMapping(value = "/{sId}")
    public AjaxResult getInfo(@PathVariable("sId") Long sId)
    {
        return AjaxResult.success(schoolService.selectSchoolBySId(sId));
    }

    /**
     * 新增学校列表
     */
    @PreAuthorize("@ss.hasPermi('school:school:add')")
    @Log(title = "学校列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody School school)
    {
    	school.setSname(school.getSname().trim());
    	school.setScode(school.getScode().trim());
        return toAjax(schoolService.insertSchool(school));
    }

    /**
     * 修改学校列表
     */
    @PreAuthorize("@ss.hasPermi('school:school:edit')")
    @Log(title = "学校列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody School school)
    {
    	school.setSname(school.getSname().trim());
    	school.setScode(school.getScode().trim());
        return toAjax(schoolService.updateSchool(school));
    }

    /**
     * 删除学校列表
     */
    @PreAuthorize("@ss.hasPermi('school:school:remove')")
    @Log(title = "学校列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sIds}")
    public AjaxResult remove(@PathVariable Long[] sIds)
    {
        return toAjax(schoolService.deleteSchoolBySIds(sIds));
    }
}
